package com.dhh.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

import com.dhh.vo.DailyEnergy;
import com.dhh.vo.FoodLog;

public class FoodLogDAO {
	private DataBaseHelper db;
	
	public FoodLogDAO(DataBaseHelper db){
		this.db = db;
	}
		
	public List<FoodLog> getFoodLog(String date){
		List<FoodLog> lst = new ArrayList<FoodLog>();
		Cursor cursor = null;
		try{
			String whereSQL = "date='"+date+"'";
			cursor = db.getReadableDatabase().query(true, "FOOD_LOG", FoodLog.getColumns(), whereSQL,null, null, null, null, null);
			if(cursor!=null && cursor.getCount()>0){
				cursor.moveToFirst();
				do{
					FoodLog log = new FoodLog();
					log.setId(cursor.getString(0));
					log.setDate(cursor.getString(1));
					log.setServing(cursor.getString(2));
					log.setFoodName(cursor.getString(3));
					log.setFoodGroup(cursor.getString(4));
					log.setEnergy(cursor.getString(5));
					log.setPeriod(cursor.getString(6));
					log.setQuatity(cursor.getString(7));
					log.setPlanEnergy(cursor.getString(8));
					lst.add(log);
				}while(cursor.moveToNext());
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(cursor!=null)
				cursor.close();
		}
		
		return lst;
	}
	
	public List<FoodLog> getFoodLogByPeriod(String period,String date){
		List<FoodLog> lst = new ArrayList<FoodLog>();
		Cursor cursor = null;
		try{
			String whereSQL = "period='"+period+"'";
			whereSQL += " and date='"+date+"'";
			cursor = db.getReadableDatabase().query(true, "FOOD_LOG", FoodLog.getColumns(), whereSQL,null, null, null, null, null);
			if(cursor!=null && cursor.getCount()>0){
				cursor.moveToFirst();
				do{
					FoodLog log = new FoodLog();
					log.setId(cursor.getString(0));
					log.setDate(cursor.getString(1));
					log.setServing(cursor.getString(2));
					log.setFoodName(cursor.getString(3));
					log.setFoodGroup(cursor.getString(4));
					log.setEnergy(cursor.getString(5));
					log.setPeriod(cursor.getString(6));
					log.setQuatity(cursor.getString(7));
					log.setPlanEnergy(cursor.getString(8));
					lst.add(log);
				}while(cursor.moveToNext());
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(cursor!=null)
				cursor.close();
		}
		
		return lst;
	} 
	
	public List<DailyEnergy> getEnergyByDate(Calendar c){
		List<DailyEnergy> lst = new ArrayList<DailyEnergy>();
		try{
			String whereSQL = lastDate(c);
			Cursor cursor = db.getReadableDatabase().query(true, "FOOD_LOG", new String[]{"date","sum(energy)","plan_energy"}, whereSQL,null, "date,plan_energy", null, "date", null);
			if(cursor!=null && cursor.getCount()>0 && cursor.moveToFirst()){
				do{
					DailyEnergy log = new DailyEnergy();
					log.setDate(cursor.getString(0));
					log.setEnergy(cursor.getString(1));
					log.setPlanEnergy(cursor.getString(2));
					lst.add(log);
				}while(cursor.moveToNext());
				cursor.close();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return lst;
	}
	
	public List<DailyEnergy> listDailyEnergy(){ 
		List<DailyEnergy> lst = new ArrayList<DailyEnergy>();
		//String whereSQL = "date>='"+date+"'";
		try{
			Cursor cursor = db.getReadableDatabase().query(true, "FOOD_LOG", new String[]{"date","sum(energy)","plan_energy"}, null,null, "date,plan_energy", null, "date desc", null);
			if(cursor!=null && cursor.getCount()>0 && cursor.moveToFirst()){
				do{
					DailyEnergy log = new DailyEnergy();
					log.setDate(cursor.getString(0));
					log.setEnergy(cursor.getString(1));
					log.setPlanEnergy(cursor.getString(2));
					lst.add(log);
				}while(cursor.moveToNext());
				cursor.close();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return lst;
	}
	
	public String lastDate(Calendar c){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String whereSQL = "date in (";
		String[] dates = new String[7];
		for(int i=0; i<7; i++){
			dates[i] = df.format(c.getTime());
			whereSQL += "'"+dates[i]+"'";
			c.add(Calendar.DAY_OF_MONTH, -1);
			
			if(i<6)
				whereSQL += ",";
		}
		whereSQL += ")";
		return whereSQL;
	}
	
	public boolean addFoodLog(FoodLog log){
		boolean isSuc = false;
		ContentValues initialValues = createContentValues(log);
		try{
			db.openDataBase();
			db.getWritableDatabase().insert("FOOD_LOG", null, initialValues);
			isSuc = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		return isSuc;
	}
	
	public boolean deleteFoodLog(String id){
		boolean isSuc = false;
		
		try{
			db.getWritableDatabase().delete("FOOD_LOG", "_id=?",new String[] {id});
			isSuc = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return isSuc;
	}
	
	private ContentValues createContentValues(FoodLog log){
		ContentValues values = new ContentValues();
		values.put(FoodLog.C_FOOD_NAME, log.getFoodName());
		values.put(FoodLog.C_FOOD_GROUP, log.getFoodGroup());
		values.put(FoodLog.C_SERVING, log.getServing());
		values.put(FoodLog.C_ENERGY, log.getEnergy());
		values.put(FoodLog.C_PERIOD, log.getPeriod());
		values.put(FoodLog.C_DATE, log.getDate());
		values.put(FoodLog.C_QUATITY, log.getQuatity());
		values.put(FoodLog.C_PLAN_ENERGY, log.getPlanEnergy());
		return values;
	}
}
